﻿using NoteHelper.IBusiness;
using NoteHelper.Utils;
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using Unity;

namespace NoteHelper.Sys
{
    public partial class Sysm003 : WeifenLuo.WinFormsUI.Docking.DockContent, IBaseView
    {
        private log4net.ILog logger;
        private DevAge.ComponentModel.IBoundList bd;
        private const string Pre = "ProjectForm_";
        public Sysm003()
        {
            InitializeComponent();
        }

        private void Sysm003_Load(object sender, EventArgs e)
        {
            logger = log4net.LogManager.GetLogger("NoteHelper.Sys.Sysm003");
            displayGrid.FixedRows = 1;
            displayGrid.FixedColumns = 1;
            CreateColumns();

            var container = UnityHelper.GetContainer();
            var projectFormManager = container.Resolve<IProjectFormManager>();
            var controller = new Sysm003Controller(this, projectFormManager);
        }
        private void CreateColumns()
        {
            DevAge.Drawing.RectangleBorder border = new DevAge.Drawing.RectangleBorder(new DevAge.Drawing.BorderLine(Color.ForestGreen), new DevAge.Drawing.BorderLine(Color.ForestGreen));
            SourceGrid.Cells.Views.Cell viewString = new SourceGrid.Cells.Views.Cell();
            viewString.BackColor = Color.DarkSeaGreen;
            viewString.Border = border;
            
            SourceGrid.DataGridColumn gridColumn;

            gridColumn = displayGrid.Columns.Add("$_rowIndex", "下标", typeof(int));
            gridColumn.Width = 50;

            gridColumn = displayGrid.Columns.Add("ObjectID", "窗体编号", typeof(string));
            gridColumn.Width = 100;

            gridColumn = displayGrid.Columns.Add("FullName", "窗体全名", typeof(string));
            gridColumn.Width = 160;

            gridColumn = displayGrid.Columns.Add("Description", "功能描述", typeof(string));
            gridColumn.Width = 300;

            gridColumn = displayGrid.Columns.Add("UpdateAt", "最后修改时间", typeof(DateTime));
            gridColumn.Width = 100;

            gridColumn = displayGrid.Columns.Add(null, "", new SourceGrid.Cells.Link());
            SourceGrid.Cells.Controllers.Button linkEvents = new SourceGrid.Cells.Controllers.Button();
            linkEvents.Executed += new EventHandler(linkEvents_Executed);
            gridColumn.DataCell.AddController(linkEvents);

            SourceGrid.Cells.Views.Link viewLink = new SourceGrid.Cells.Views.Link();
            viewLink.BackColor = Color.DarkSeaGreen;
            viewLink.ImageAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            viewLink.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            viewLink.Border = border;
            gridColumn.DataCell.View = viewLink;
            ((SourceGrid.Cells.Link)gridColumn.DataCell).Image = GlobalResource.打开ToolStripButton_Image;
            gridColumn.Width = 30;
            
            foreach (SourceGrid.DataGridColumn col in displayGrid.Columns)
            {
                if (col.PropertyName != null) 
                    col.DataCell.View = viewString;
                SourceGrid.Conditions.ICondition condition =
                    SourceGrid.Conditions.ConditionBuilder.AlternateView(col.DataCell.View,
                                                                         Color.LightGray, Color.Black);
                col.Conditions.Add(condition);
            }
        }

        private void linkEvents_Executed(object sender, EventArgs e)
        {
            var cellContext = (SourceGrid.CellContext)sender;
            SourceGrid.DataGrid grid = (SourceGrid.DataGrid)cellContext.Grid;
            int rowIndex = cellContext.Position.Row;
            grid.Selection.SelectRow(rowIndex, true);
            object[] rows = displayGrid.SelectedDataRows;

            DataRowView row = (DataRowView)rows[0];
            string className = row["FullName"].ToString();
            DockHandler.ShowPanel(className);
        }

        #region ==== IBaseView Impl ====

        private DataTable _displayList;
        public DataTable DisplayList
        {
            get { return _displayList; }
            set
            {
                if (value == null) return;
                _displayList = value;

                bd = new DevAge.ComponentModel.BoundDataView(_displayList.DefaultView);
                bd.AllowNew = false;
                bd.AllowEdit = false;
                bd.AllowDelete = false;
                displayGrid.DataSource = bd;
            }
        }
        public FormCommand Cmd { get; set; }
        public Hashtable RowData
        {
            get
            {
                return splitContainer1.Panel1.GetData(Pre);
            }
            set
            {
                splitContainer1.Panel1.SetData(Pre, value);
            }
        }
        public event EventHandler Insert;
        public event EventHandler Edit;
        public event EventHandler Remove;
        public event EventHandler Copy;
        public event EventHandler Query;
        public event EventHandler Accept;
        public event EventHandler Cancel;
        public event EventHandler Print;
        public event EventHandler Help;
        public void BeginEdit()
        {
            splitContainer1.Panel1.ChangeInputState(Pre, true);
        }
        public void EndEdit()
        {
            splitContainer1.Panel1.ChangeInputState(Pre, false);
        }
        public int GetRowIndex()
        {
            int rowIndex = -1;
            object[] rows = displayGrid.SelectedDataRows;
            if (rows != null && rows.Length > 0)
            {
                DataRowView row = (DataRowView)rows[0];
                int.TryParse(row["$_rowIndex"].ToString(), out rowIndex);
            }
            return rowIndex;
        }
        #endregion

        #region ==== Toolbar Event ====
        /// <summary>
        /// 添加
        /// </summary>
        private void InsertButtonClick(object sender, EventArgs e)
        {
            Insert?.Invoke(sender, e);
        }
        /// <summary>
        /// 修改
        /// </summary>
        private void EditButtonClick(object sender, EventArgs e)
        {
            Edit?.Invoke(sender, e);
        }
        /// <summary>
        /// 删除
        /// </summary>
        private void RemoveButtonClick(object sender, EventArgs e)
        {
            if (Remove == null || GetRowIndex() == -1) return;

            if (System.Windows.Forms.MessageBox.Show(this, displayGrid.DeleteQuestionMessage, System.Windows.Forms.Application.ProductName, System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                Remove.Invoke(sender, e);
            }
        }
        /// <summary>
        /// 复制
        /// </summary>
        private void CopyButtonClick(object sender, EventArgs e)
        {
            Copy?.Invoke(sender, e);
        }
        /// <summary>
        /// 查询
        /// </summary>
        private void QueryButtonClick(object sender, EventArgs e)
        {
            Query?.Invoke(sender, e);
        }
        /// <summary>
        /// 确定
        /// </summary>
        private void AcceptButtonClick(object sender, EventArgs e)
        {
            Accept?.Invoke(sender, e);
        }
        /// <summary>
        /// 取消
        /// </summary>
        private void CancelButtonClick(object sender, EventArgs e)
        {
            Cancel?.Invoke(sender, e);
        }
        /// <summary>
        /// 打印
        /// </summary>
        private void PrintButtonClick(object sender, EventArgs e)
        {
            Print?.Invoke(sender, e);
        }
        /// <summary>
        /// 帮助
        /// </summary>
        private void HelpButtonClick(object sender, EventArgs e)
        {
            Help?.Invoke(sender, e);
        }

        public void Notify(string notification)
        {
            AlterMsg.Info(notification);
        }
        #endregion
    }
}
